package com.qianfeng.plateform2203.zuul.mq;

import com.qianfeng.plateform2203.common.mq.APIRoutingType;
import com.qianfeng.plateform2203.common.mq.ApiRoutingMQBean;
import com.qianfeng.plateform2203.zuul.feign.CacheService;
import com.qianfeng.plateform2203.zuul.localcache.APIRoutringCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消费者
 */
@Component
public class APIRoutingStreamListener {

    @Autowired
    CacheService cacheService;


    @StreamListener("apiroutingchange")
    public void onMessage(ApiRoutingMQBean mqBean) throws Exception {

        final APIRoutingType type = mqBean.getType();
        final String key = mqBean.getKey();

        switch (type){
            case DELETE:
                // 把本地缓存中的路由信息删除掉
                APIRoutringCache.localcache.remove(key);
                break;
            case ADD:
            case UPDATE:
                // 更新本地缓存
                final Map<Object, Object> apiRoutingInfo = cacheService.hGetAll(key);
                APIRoutringCache.localcache.put(key,apiRoutingInfo);
                break;

        }


    }
}
